package model

import (
	"cms/libs"
	"errors"
	"github.com/jinzhu/gorm"
	config "github.com/spf13/viper"
	"math"
	"time"
)

type Bt struct {
	gorm.Model
	Name        string    `gorm:"type:varchar(500); NOT NULL; DEFAULT '';"validate:"required"`
	Keywords    string    `gorm:"type:varchar(250); NOT NULL; DEFAULT '0';"validate:"required"`
	Length      uint      `gorm:"type:bigint(20);NOT NULL; DEFAULT '0';"validate:"required"`
	PieceLength uint      `gorm:"type:int(11);NOT NULL; DEFAULT '0';"validate:"required"`
	Infohash    string    `gorm:"type:char(40);DEFAULT '';"`
	Files       string    `gorm:"type:text;DEFAULT '';"`
	Hits        int       `gorm:"type:int(11); NOT NULL; DEFAULT '0';"validate:"number,min=0"`
	Hot         int       `gorm:"type:int(11); NOT NULL; DEFAULT '0';"validate:"number,min=0"`
	LastTime    time.Time `gorm:"type:timestamp;DEFAULT '';"`
}

func (this *Bt) List(page int) ([]Bt, int, int) {
	var data = []Bt{}
	var totalCount int
	db := libs.DB

	limit := config.GetInt("pagination.PageSize")
	offset := (page - 1) * limit
	db.Find(&data).Count(&totalCount)
	db.Offset(offset).Limit(limit).Order("id desc").Find(&data)
	totalPages := int(math.Ceil(float64(totalCount) / float64(limit)))
	return data, totalCount, totalPages
}

func (this *Bt) ListSiteMap(page int) ([]Bt, int, int) {
	var data = []Bt{}
	var totalCount int
	db := libs.DB
	limit := 1000
	offset := (page - 1) * limit
	db.Find(&data).Count(&totalCount)
	db.Offset(offset).Limit(limit).Order("id desc").Find(&data)
	totalPages := int(math.Ceil(float64(totalCount) / float64(limit)))
	return data, totalCount, totalPages
}

func (this *Bt) SearchList(ids []string) []Bt {
	var data = []Bt{}
	db := libs.DB
	db.Select("id,name,keywords,length,piece_length,infohash,hits,hot,last_time,created_at").Where("id in (?)", ids).Order("id desc").Find(&data)
	return data
}

func (this *Bt) Info(id uint) (Bt, error) {
	var bt Bt
	db := libs.DB

	if db.Where("id = ?", id).First(&bt).RecordNotFound() {
		return Bt{}, errors.New("内容未找到")
	}
	return bt, nil
}

func (this *Bt) Add(postValues map[string][]string) error {
	var bt Bt
	db := libs.DB

	if err := libs.FormDecode(&bt, postValues); err != nil {
		return err
	}
	if err := libs.Validate(bt); err != nil {
		return err
	}
	if !db.Where("name = ? ", bt.Name).First(&Bt{}).RecordNotFound() {
		return errors.New("该标题已经存在")
	}

	if err := db.Create(&bt).Error; err != nil {
		return err
	}
	return nil
}

func (this *Bt) Update(postValues map[string][]string) error {
	var bt Bt
	db := libs.DB

	if err := libs.FormDecode(&bt, postValues); err != nil {
		return err
	}
	if err := libs.Validate(bt); err != nil {
		return err
	}
	if !db.Where("name = ? and id != ?", bt.Name, bt.ID).First(&Bt{}).RecordNotFound() {
		return errors.New("该标题已经存在")
	}
	if db.Where("id = ? ", bt.ID).First(&Bt{}).RecordNotFound() {
		return errors.New("未查询到内容id")
	}

	if err := db.Save(&bt).Error; err != nil {
		return err
	}
	return nil
}

func (this *Bt) Del(id uint) error {
	var bt Bt
	db := libs.DB

	if err := db.Where("id = ?", id).Delete(&bt).Error; err != nil {
		return err
	}
	return nil
}
